package easy;

import java.util.HashSet;
import java.util.Set;

public class Solution_929 {

    public int numUniqueEmails(String[] emails) {
        StringBuffer buffer = new StringBuffer();
        Set<String> set = new HashSet<>();
        for (String email : emails) {
            char[] chars = email.toCharArray();
            boolean before = true, ignore = false;
            for (int i = 0; i < chars.length; i++) {
                if (chars[i] == '@') {
                    before = false;
                    buffer.append(chars[i]);
                    continue;
                } else {
                    if (before) {
                        if (chars[i] == '+') {
                            ignore = true;
                        } else if (!ignore && chars[i] != '.') {
                            buffer.append(chars[i]);
                        }
                    } else {
                        buffer.append(chars[i]);
                    }
                }
            }
            System.out.println(buffer.toString());
            set.add(buffer.toString());
            buffer.setLength(0);
        }
        return set.size();
    }

    public static void main(String[] args) {
        Solution_929 model = new Solution_929();
        System.out.println(model.numUniqueEmails(
            new String[]{"test.email+alex@leetcode.com", "test.e.mail+bob.cathy@leetcode.com",
                "testemail+david@lee.tcode.com"}));
    }
}
